home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Code Resources / 3D Buttons CDEF 1.0b4 / Source / 3D Buttons CDEF source / (3D Buttons CDEF.π) / UGBDraw.cp < prev    next >
Encoding:
Text File  |  1994-07-31  |  4.9 KB  |  167 lines  |  [TEXT/MMCC]

  1. /**************************************************************************
  2.     UGBDraw
  3.     
  4.     Public domain, by Zig Zichterman.
  5.     
  6.     Utility functions for drawing in color. All these functions assume
  7.     color quickdraw.
  8.     
  9.     This class implements 3D drawing according to the guidelines
  10.     suggested in _develop_ 15. Some of the drawing code is taken from
  11.     the public domain source accompanying _develop_ 15.
  12.  
  13.     07/31/94    zz    add ColorQDIsPresent()
  14.     07/28/94    zz    add offscreen drawing stuff
  15. **************************************************************************/
  16. #include "UGBDraw.h"
  17.  
  18. #include <GestaltEqu.h>
  19.  
  20. /**************************************************************************
  21.     PenNormal()
  22.     
  23.     Restore the pen color to black on grety
  24. **************************************************************************/
  25. void
  26. UGBDraw::PenNormal(void)
  27. {
  28.     ForeGrey(UGBDraw_black);
  29.     BackGrey(UGBDraw_background);
  30. }
  31.  
  32. /**************************************************************************
  33.     PenReallyNormal()
  34.     
  35.     Restore the pen color to black on white
  36. **************************************************************************/
  37. void
  38. UGBDraw::PenReallyNormal(void)
  39. {
  40.     ForeGrey(UGBDraw_black);
  41.     BackGrey(UGBDraw_white);
  42. }
  43.  
  44. /**************************************************************************
  45.     ForeGrey()
  46.     
  47.     Set the pen to a grey level.
  48. **************************************************************************/
  49. void
  50. UGBDraw::ForeGrey(unsigned short inGrey)
  51. {
  52.     RGBColor    color;
  53.     color.red = color.green = color.blue = inGrey;
  54.     ::RGBForeColor(&color);
  55. }
  56.  
  57.  
  58. /**************************************************************************
  59.     BackGrey()
  60.     
  61.     Set the pen to a grey level.
  62. **************************************************************************/
  63. void
  64. UGBDraw::BackGrey(unsigned short inGrey)
  65. {
  66.     RGBColor    color;
  67.     color.red = color.green = color.blue = inGrey;
  68.     ::RGBBackColor(&color);
  69. }
  70.  
  71. /**************************************************************************
  72.     OffscreenPre()
  73.     
  74.     Set up an offscreen GWorld for the current clip rect
  75. **************************************************************************/
  76. void
  77. UGBDraw::OffscreenPre(Offscreen &outOffscreen)
  78. {
  79.     // clear out the gworld field in case we fail for some reason
  80.     outOffscreen.gworld = NULL;
  81.  
  82.     // save the old settings
  83.     ::GetGWorld(&outOffscreen.savePort,&outOffscreen.saveDevice);
  84.     
  85.     {    // get the current clip rgn's bounds
  86.         RgnHandle    rgn = ::NewRgn();
  87.         if (!rgn) return;
  88.         ::GetClip(rgn);
  89.         outOffscreen.lbounds = (**rgn).rgnBBox;
  90.         ::DisposeRgn(rgn);
  91.     
  92.         // convert the bounds from local to global
  93.         outOffscreen.gbounds = outOffscreen.lbounds;
  94.         ::LocalToGlobal(&topLeft(outOffscreen.gbounds));
  95.         ::LocalToGlobal(&botRight(outOffscreen.gbounds));
  96.         
  97.     }
  98.     
  99.     // create an offscreen GWorld in temporary memory with the
  100.     // given bounds. We promise to release the temp memory
  101.     // before the user notices its gone.
  102.     OSErr    err    = ::NewGWorld(&outOffscreen.gworld,0,&outOffscreen.gbounds,
  103.                                 NULL,NULL,useTempMem);
  104.     if (err || !outOffscreen.gworld) {
  105.         outOffscreen.gworld = NULL;
  106.         return;
  107.     }
  108.     
  109.     // switch drawing over to offworld city
  110.     ::SetGWorld(outOffscreen.gworld,NULL);
  111.     
  112.     // make sure drawing code doesn't notice the difference
  113.     ::SetOrigin(outOffscreen.lbounds.left,outOffscreen.lbounds.top);
  114.     
  115.     // ALWAYS LockPixels() before drawing. If you ever have problems 
  116.     // with your offscreen drawing code, checking LockPixels() is the
  117.     // first thing you should check. At least that's been my experience...
  118.     ::LockPixels(::GetGWorldPixMap(outOffscreen.gworld));
  119.     
  120.     // clean the slate
  121.     ::EraseRect(&outOffscreen.lbounds);
  122. }
  123.  
  124. /**************************************************************************
  125.     OffscreenPost()
  126.     
  127.     Copy an offscreen GWorld back to the screen and nuke it
  128. **************************************************************************/
  129. void
  130. UGBDraw::OffscreenPost(Offscreen &ioOffscreen)
  131. {
  132.     if (!ioOffscreen.gworld) return;    // nothing to copy from, drawing
  133.                                         // went directly to the screen
  134.                                         
  135.     // restore drawing to the screen
  136.     ::SetGWorld(ioOffscreen.savePort,ioOffscreen.saveDevice);
  137.     
  138.     // copy the gworld to the screen. Oooh. Aaah.
  139.     ::ForeColor(blackColor);
  140.     ::BackColor(whiteColor);
  141.     ::CopyBits(&((GrafPtr) ioOffscreen.gworld)->portBits,
  142.                 &((GrafPtr) ioOffscreen.savePort)->portBits,
  143.                 &ioOffscreen.lbounds,&ioOffscreen.lbounds,
  144.                 srcCopy,NULL);
  145.     
  146.     // release the gworld. I don't think I really need to unlock
  147.     // the pixels before disposing it, but what can it hurt?
  148.     ::UnlockPixels(::GetGWorldPixMap(ioOffscreen.gworld));
  149.     ::DisposeGWorld(ioOffscreen.gworld);
  150.     ioOffscreen.gworld = NULL;
  151. }
  152.  
  153. /**************************************************************************
  154.     ColorQDIsPresent()
  155. **************************************************************************/
  156. Boolean
  157. UGBDraw::ColorQDIsPresent(void)
  158. {
  159.     Boolean    present = false;
  160.     long    result;
  161.     OSErr    err = ::Gestalt(gestaltQuickdrawFeatures,&result);
  162.     if (!err && ((result & (1L << gestaltHasColor)) != 0)) {
  163.         present = true;
  164.     }
  165.     return present;
  166. }
  167.